/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.app.dao.business.CereBusinessBuyerUserDAO;
import com.shop.cereshop.app.service.business.CereBusinessBuyerUserService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.business.CereBusinessBuyerUser;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CereBusinessBuyerUserServiceImpl extends ServiceImpl<CereBusinessBuyerUserDAO,CereBusinessBuyerUser> implements CereBusinessBuyerUserService {

    @Autowired
    private CereBusinessBuyerUserDAO cereBusinessBuyerUserDAO;


    @Override
    public CereBusinessBuyerUser queryByShopIdAndByBuyerUserId(Long shopId, Long buyUserId)throws CoBusinessException{
        LambdaQueryWrapper<CereBusinessBuyerUser> q1 = new LambdaQueryWrapper<>();
        q1.eq(CereBusinessBuyerUser::getShopId, shopId);
        q1.eq(CereBusinessBuyerUser::getBuyerUserId, buyUserId);
        List<CereBusinessBuyerUser> list = list(q1);
        if (CollectionUtil.isNotEmpty(list)){
            if (list.size() > 2){
                //数据库错误，逻辑异常
                throw new CoBusinessException(CoReturnFormat.LOGIC_ERROR_DATABASE);
            }
            return list.get(0);
        }
        return null;
    }

    @Override
    public void addBusinessBuyerUser(CereBusinessBuyerUser cereBusinessBuyerUser) {
        cereBusinessBuyerUser.setCreateTime(TimeUtils.yyMMddHHmmss());
        cereBusinessBuyerUserDAO.insertOrUpdate(cereBusinessBuyerUser);
    }

    @Override
    public void refreshUpdateTime(CereBusinessBuyerUser bbu) {
        cereBusinessBuyerUserDAO.refreshUpdateTime(bbu);
    }

    @Override
    public List<CereBuyerUser> selectBuyerUserByShopId(Long shopId) {
        return cereBusinessBuyerUserDAO.selectBuyerUserByShopId(shopId);
    }
}
